package cn.tx.controller;

import cn.tx.dao.InputDao;
import cn.tx.model.Clazz;
import cn.tx.model.Grade;
import cn.tx.model.GradeYear;
import cn.tx.model.Input;
import cn.tx.page.Page;
import cn.tx.service.ClazzService;
import cn.tx.service.GradeService;
import cn.tx.service.InputService;
import cn.tx.util.StringUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.xml.soap.Name;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/input")
public class InputController {
    @Autowired
    private InputService inputService;

    /*首页展示*/
    @RequestMapping(path = "/list")
    public ModelAndView list(ModelAndView modelAndView) {
        modelAndView.setViewName("input/input_list");
        // List<Input> inputList = inputService.findAll();
        // modelAndView.addObject("inputList",inputList);
        // modelAndView.addObject("inputListJson", JSONArray.fromObject(inputList));
        return modelAndView;
    }


    /*展示书列表*/
    @RequestMapping(path = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getList(
            @RequestParam(value = "name", required = false, defaultValue = "") String name,

            @RequestParam(value = "author", required = false, defaultValue = "") String author,
            @RequestParam(value = "input", required = false, defaultValue = "") String input,
            @RequestParam(value = "date", required = false, defaultValue = "") String date,
            @RequestParam(value = "number", required = false, defaultValue = "") String number,
            @RequestParam(value = "type", required = false, defaultValue = "") String type,
            @RequestParam(value = "community", required = false, defaultValue = "") String community,
            Page page
    ) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> querymap = new HashMap<String, Object>();
        querymap.put("name", "%" + name + "%");
        if (name != null) {
            querymap.put("name", name);
        }
        querymap.put("offset", page.getOffset());
        querymap.put("pageSize", page.getRows());
        map.put("rows", inputService.findList(querymap));
        map.put("total", inputService.findTotal(querymap));
        return map;
    }





    /*添加书籍*/
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Input input) {
        Map<String, String> map = new HashMap<>();
        if (StringUtils.isEmpty(input.getName())) {
            map.put("type", "error");
            map.put("msg", "书籍不能为空");
            return map;
        }

        if (inputService.addInput(input) <= 0) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "添加成功");
        return map;
    }


    /*修改*/
    @RequestMapping(path = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Input input) {
        Map<String, String> map = new HashMap<>();
        Input name = inputService.findByName(input.getName());
        if (name!=null){
            if (input.getId()!=name.getId()){
                map.put("type", "error");
                map.put("msg", "id不匹配");
                return map;
            }
        }
        if (input.getName()==null){
            map.put("type", "error");
            map.put("msg", "书名不能为空");
            return map;
        }
        System.out.println(input);
        if (inputService.UpdateInputById(input)<= 0) {
            map.put("type", "error");
            map.put("msg", "修改失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "修改成功");
        return map;
    }



    /*删除书籍*/
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value = "ids[]",required = true) Long[] ids
    ) {
        Map<String, String> map = new HashMap<>();
        if (ids==null||ids.length==0){
            map.put("type", "error");
            map.put("msg", "请至少选择一条数据");
            return map;
        }
        try {
            if(inputService.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
                map.put("type", "error");
                map.put("msg", "删除失败！");
                return map;
            }
        } catch (Exception e) {
            map.put("type", "error");
            map.put("msg", "该年份下存在书籍信息，请勿冲动！");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "删除成功");
        return map;
    }



}

